package cn.easybuy.servlet.Cart;

import cn.easybuy.entity.BuyCar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/buyCarDelServlet")
public class BuyCarDelServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int entityId = Integer.parseInt(req.getParameter("entityId"));

        HttpSession session = req.getSession();
        List<BuyCar> list = (List<BuyCar> )session.getAttribute("listCarSession");
        List<BuyCar> listNew = new ArrayList<>();
        float sum=0;
        for (BuyCar buyCar : list) {
            int id = buyCar.getId();
            if(entityId!=id){
                listNew.add(buyCar);
                sum+=buyCar.getSum();
            }
        }
        session.setAttribute("listCarSession",listNew);
        session.setAttribute("sum",sum);
        session.setAttribute("size",listNew.size());
        req.getRequestDispatcher("pre/settlement/toSettlement.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
